import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Calendar;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.ComboBoxEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class iCalGUI implements ActionListener {

	public static String[] results = new String[19];
	private static JButton generate;
	private Container pane;
	private JFrame frame;
	private static JTextField subject;
	private static JTextField location;
	private static JTextField latitude;
	private static JTextField longitude;
	private static JTextField startMonth;
	private static JTextField startDay;
	private static JTextField startYear;
	private static JTextField startHour;
	private static JTextField startMin;
	private static JTextField endMonth;
	private static JTextField endDay;
	private static JTextField endYear;
	private static JTextField endHour;
	private static JTextField endMin;
	private static JTextField priority;

	private JRadioButton recurringEvent, onetimeEvent; 
	private static JComboBox classification;
	private static JComboBox TZID;

	private static JLabel message;
	private static JTextArea note;
	private static DateBox startDate;
	private static DateBox endDate;
	private ButtonGroup bGroup = new ButtonGroup();
	private GridBagConstraints c;
	private JPanel startTimeBox, endTimeBox;

	public iCalGUI() {
		createGUI();
	}

	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new iCalGUI();
			}
		});
	}

	/**
	 * Draw up GUI and add components
	 */
	private void createGUI() {
		//Create and set up the window.
		frame = new JFrame(".ics File Event Generator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		pane = frame.getContentPane();

		//Set up the content pane.
		addComponents(pane);


		//Display the window.
		frame.pack();
		frame.setVisible(true);


	}

	/**
	 * add individual components to the frame
	 * @param pane
	 */
	private void addComponents(Container pane) {
		generate = new JButton("Generate");
		generate.addActionListener(this);

		//set layout orientation
		pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		pane.setLayout(new GridBagLayout());

		c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.ipady = 10; 

		c.weighty = 1.0;
		c.gridwidth = 2;
		addLabel("Enter your event information to generate your .ics file.", 0, c, pane);

		c.gridwidth = 1;
		c.weighty = 0;

		addLabel("Subject: ", 1, c, pane);
		subject = addTextField(1, c, pane);
		addLabel("Location:    ", 2, c, pane);
		location = addTextField(2, c, pane);

		addLabel("Geo. Position: ", 3, c, pane);

		c.gridx = 1;
		c.gridy = 3;

		pane.add(addGeoBox(), c);

		JComponent radio = addRadioButton();
		c.gridx = 0;
		c.gridy = 4;
		c.gridwidth = 2;
		pane.add(radio, c);

		c.gridwidth = 1;

		addLabel("Start time:   ", 5, c, pane);

		TZID  = TZID();		
		startTimeBox = new JPanel();
		startDate = new DateBox(0);	
		startTimeBox.add(TZID);
		startTimeBox.add(startDate);
		c.gridx = 1;
		c.gridy = 5;
		pane.add(startTimeBox, c);	

		addLabel("End time:  ", 6, c, pane);


		endTimeBox = new JPanel();
		endDate = new DateBox(0);	
		endTimeBox.add(Box.createHorizontalStrut(155));
		endTimeBox.add(endDate);
		c.gridx = 1;
		c.gridy = 6;
		pane.add(endTimeBox, c);

		addLabel("Note:  ", 7, c, pane);		

		note = new JTextArea(10, 0);
		note.setLineWrap(true);
		note.setBorder(BorderFactory.createLineBorder(Color.black));
		c.gridx = 0;
		c.gridy = 8;
		c.gridwidth = 2;
		pane.add(note, c);

		JComponent lastRow = customizedRow();
		c.gridx = 0;
		c.gridy = 9;
		pane.add(lastRow, c);

		message = new JLabel("Please fill in the details...");
		message.setFont(new Font("Serif", Font.BOLD, 16));
		c.gridx = 0;
		c.gridy = 10;
		pane.add(message, c);
	}


	/**
	 * Add time zone options
	 * @return
	 */
	private JComboBox TZID() {
		String[] timeZone = {
				"Pacific/Honolulu",
				"America/Chicago",
				"America/New_York",
				"America/Denver",
				"America/Los_Angeles"				
		};

		JComboBox list = new JComboBox(timeZone); 
		list.setEditable(false);
		return list;
	}

	/**
	 * Geo position box, determines valid coordinate
	 * @return
	 */
	private JPanel addGeoBox() {
		JPanel temp = new JPanel(new GridBagLayout());		

		JLabel open = new JLabel("  (  ");
		JLabel close = new JLabel("  )");
		JLabel comma = new JLabel("  ,  ");
		latitude = new JTextField(5);
		longitude = new JTextField(5);

		//determines rather input is valid 
		latitude.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void changedUpdate(DocumentEvent e) {

			}
			@Override
			public void insertUpdate(DocumentEvent e) {
				try {
					if ((Double.parseDouble(latitude.getText()) < -90 || Double.parseDouble(latitude.getText()) > 90)) {
						message.setText("Please enter a valid coordinate");
						message.setForeground(Color.red);
						generate.setEnabled(false);
					} else {
						continueMessage();
					}	
				} catch (Exception exp) {
					message.setText("Please enter a valid coordinate");
					message.setForeground(Color.red);
					generate.setEnabled(false);
				}
			}
			@Override
			public void removeUpdate(DocumentEvent e) {
				try {
					if ((Double.parseDouble(latitude.getText()) < -90 || Double.parseDouble(latitude.getText()) > 90)) {
						message.setText("Please enter a valid coordinate");
						message.setForeground(Color.red);
						generate.setEnabled(false);
					} else {
						continueMessage();
					}	
				} catch (Exception exp) {
					if (latitude.getText().length() == 0) {
						continueMessage();
					} else {
						message.setText("Please enter a valid coordinate");
						message.setForeground(Color.red);
						generate.setEnabled(false);
					}
				}
			}

		});

		//determines rather input is valid 
		longitude.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void changedUpdate(DocumentEvent e) {

			}
			@Override
			public void insertUpdate(DocumentEvent e) {
				try {
					if ((Double.parseDouble(longitude.getText()) < -180 || Double.parseDouble(longitude.getText()) > 180)) {
						message.setText("Please enter a valid coordinate");
						message.setForeground(Color.red);
						generate.setEnabled(false);
					} else {
						continueMessage();
					}	
				} catch (Exception exp) {
					message.setText("Please enter a valid coordinate");
					message.setForeground(Color.red);
					generate.setEnabled(false);
				}
			}
			@Override
			public void removeUpdate(DocumentEvent e) {
				try {
					if ((Double.parseDouble(longitude.getText()) < -180 || Double.parseDouble(longitude.getText()) > 180)) {
						message.setText("Please enter a valid coordinate");
						message.setForeground(Color.red);
						generate.setEnabled(false);
					} else {
						continueMessage();
					}	
				} catch (Exception exp) {
					if (longitude.getText().length() == 0) {
						continueMessage();
					} else {
						message.setText("Please enter a valid coordinate");
						message.setForeground(Color.red);
						generate.setEnabled(false);
					}
				}
			}

		});


		temp.add(open);	
		temp.add(latitude);		
		temp.add(comma);		
		temp.add(longitude);		
		temp.add(close);

		return temp;
	}


	/**
	 * Add radio button for One-time or recurring event option
	 * @return
	 */
	private JPanel addRadioButton() {
		JPanel pane = new JPanel();
		onetimeEvent = new JRadioButton("One-time Event", true);
		onetimeEvent.addActionListener(this);
		recurringEvent = new JRadioButton("Recurring Event", false);
		recurringEvent.addActionListener(this);

		pane.add(onetimeEvent);
		pane.add(recurringEvent);
		bGroup.add(onetimeEvent);
		bGroup.add(recurringEvent);
		return pane;
	}

	/**
	 * Add JLabel to the panel 
	 * @param title Title of the Label
	 * @param row At what row of the panel should the JLabel be added 
	 * @param c 
	 * @param pane
	 */
	private void addLabel(String title, int row, GridBagConstraints c, Container pane) {
		JLabel label = new JLabel(title);
		c.gridx = 0;
		c.gridy = row;
		pane.add(label, c);
	}

	/**
	 * Add JTextField to the panel 
	 * @param y At what column should the JTextField be added
	 * @param c
	 * @param pane
	 * @return
	 */
	private JTextField addTextField(int y, GridBagConstraints c, Container pane) {
		JTextField tf = new JTextField(20);
		c.gridx = 1;	
		c.gridy = y;
		pane.add(tf, c);
		return tf;
	}


	/**
	 * Add priority box to the panel 
	 * @return
	 */
	private JComboBox priority() {
		String[] level = new String[10];
		for (int i = 0; i < 10; i ++) {
			level[i] = "" + i;
		}

		JComboBox list = new JComboBox(level); 
		list.setEditable(true);
		ComboBoxEditor editor = list.getEditor();
		priority = (JTextField)editor.getEditorComponent();
		priority.setColumns(3);		
		return list;
	}


	/**
	 * Add classification (public, private, confidential) to the panel 
	 * @return
	 */
	private JComboBox classification() {
		String[] classes = {
				"PUBLIC",
				"PRIVATE",
				"CONFIDENTIAL"			
		};

		classification = new JComboBox(classes); 
		return classification;
	}

	/**
	 * Combine components to one component for layout
	 * @param pane
	 * @return
	 */
	private JPanel customizedRow() {
		JPanel panel = new  JPanel();

		JComboBox priority = priority();
		JComboBox classification = classification();
		JLabel label = new JLabel("Priority:   ");
		panel.add(classification);
		panel.add(label);
		panel.add(priority);
		panel.add(generate);
		return panel;		
	}

	/**
	 * Store the info typed in to variables. 
	 */
	private static void update(int mode) {
		startDate.substitute(true);
		endDate.substitute(false);

		String tempStartDay = null;
		String tempEndDay = null;
		String tempStartMonth = null;
		String tempEndMonth = null;
		String tempStartYear = null;
		String tempEndYear = null;
		if (mode == 0) {
			tempStartDay = startDay.getText();
			tempEndDay = endDay.getText();

			//make sure the date is in "dd" format, add leading 0 if it's not.
			if (tempStartDay.length() != 2) {
				String temp = "0" + tempStartDay;
				tempStartDay = temp;
			}

			if (tempEndDay.length() != 2) {

				String temp = "0" + tempEndDay;
				tempEndDay = temp;
			}

			tempStartMonth = startMonth.getText();
			tempEndMonth = endMonth.getText();
			tempStartYear = startYear.getText();
			tempEndYear = endYear.getText();
		}


		//store everything in array for easier variable passing to second class
		results[0] = subject.getText();
		results[1] = location.getText();
		results[2] = tempStartMonth;
		results[3] = tempStartDay;
		results[4] = tempStartYear;
		results[5] = startHour.getText();
		results[6] = startMin.getText();
		results[7] = tempEndMonth;
		results[8] = tempEndDay;
		results[9] = tempEndYear;
		results[10] = endHour.getText();
		results[11] = endMin.getText();
		results[12] = note.getText();
		results[13] = (String) classification.getSelectedItem();
		results[14] = priority.getText();
		results[15] = startDate.getRecurringWeekdays();
		if (latitude.getText().equals("") && longitude.getText().equals("")) {  //UH Post building default
			results[16] = "21.297180"; 
			results[17] = "-157.815916";
		} else {
			results[16] = latitude.getText();
			results[17] = longitude.getText();
		}
		results[18] = (String)TZID.getSelectedItem();

	}


	/**
	 * make sure the start time is before end time 
	 * @return true if start is before end, false otherwise
	 */
	private static boolean checkTime(int mode) {
		double startTime, endTime;
		update(mode);
		GenerateFile.readResults(results);
		if (mode == 0) {		

			//combine the year month date hour min into one number for easier analysis 
			startTime = Double.parseDouble(GenerateFile.fixedResults[4] + GenerateFile.fixedResults[2] + GenerateFile.fixedResults[3] + 
					GenerateFile.fixedResults[5] + GenerateFile.fixedResults[6]);
			endTime = Double.parseDouble(GenerateFile.fixedResults[9] + GenerateFile.fixedResults[7] + GenerateFile.fixedResults[8] + 
					GenerateFile.fixedResults[10] + GenerateFile.fixedResults[11]);		

		} else {
			startTime = Double.parseDouble(GenerateFile.fixedResults[5] + GenerateFile.fixedResults[6]);
			endTime = Double.parseDouble(GenerateFile.fixedResults[10] + GenerateFile.fixedResults[11]);	
		}


		if (startTime - endTime > 0) {
			return false;
		}

		return true;
	}

	/**
	 * Default message if everything is correct 
	 */
	private static void continueMessage() {
		message.setText("Please fill in the details...");
		message.setForeground(Color.black);	
		generate.setEnabled(true);			
	}

	/**
	 * Corresponding methods when the Button Generate is pressed 
	 */
	public void actionPerformed(ActionEvent ae) {
		if (ae.getActionCommand().equals("Generate")){
			update(startDate.getMode());
			GenerateFile.readResults(results);

			try {
				GenerateFile.write(startDate.getMode());
				message.setText(".ics File Generated.");
				message.setForeground(Color.blue);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}



		} else if (ae.getActionCommand().equals("Recurring Event") || ae.getActionCommand().equals("One-time Event")) {
			startTimeBox.remove(startDate);
			endTimeBox.remove(endDate);

			if (ae.getActionCommand().equals("Recurring Event")) {
				startDate = new DateBox(1);
				endDate = new DateBox(-1);

				startTimeBox.add(startDate);			
				endTimeBox.add(endDate);

				frame.pack();
				frame.validate();

			} else {
				startDate = new DateBox(0);
				endDate = new DateBox(0);

				startTimeBox.add(startDate);			
				endTimeBox.add(endDate);

				frame.pack();
				frame.validate();
			}
		}
	}


	/**
	 * class that deals with the Dates
	 * @author MC
	 *
	 */
	static class DateBox extends JPanel implements ActionListener {

		private GridBagConstraints c;

		private JComboBox monthsList;
		private static JComboBox daysList;
		private JComboBox yearsList;
		private JComboBox hoursList;
		private JComboBox minsList;

		private JCheckBox[] weekdays = new JCheckBox[7];

		private JTextField timeMonth, timeDay, timeYear, timeHour, timeMin;

		private Calendar now = Calendar.getInstance();

		private static String currentMonth;

		private static String currentDate;

		private static String currentYear;

		private static String currentHour;

		private static String currentMin;

		private int mode;

		private static String[] longMonth = {
			"Jan",
			"Mar",
			"May",
			"Jul",
			"Aug",
			"Oct",
			"Dec"
		};

		private static String[] shortMonth = {
			"Apr",
			"Jun",
			"Sep",
			"Nov"					
		};

		/**
		 * Add month, days, years, hours, minutes boxes to the panel.
		 */
		private DateBox(int mode) {
			setLayout(new GridBagLayout());
			this.mode = mode;
			c = new GridBagConstraints();
			c.fill = GridBagConstraints.HORIZONTAL;
			c.weightx = 0;

			if (mode == 0) {
				addMonths();
				addDays();
				addYears();
			} else if (mode == -1) {
				add(Box.createHorizontalStrut(195));
			} else if (mode == 1) {
				addWeekdays();
			}

			c.gridx = 3;
			add(new JLabel("    "));

			addHours();

			c.gridx = 5;
			add(new JLabel("  :  "));

			addMinutes();
		}

		public int getMode() {
			return mode;
		}

		public String getRecurringWeekdays() {
			if (mode == 1) {
				String selected = "";
				for (int i = 0; i < 7; i ++) {
					if (weekdays[i].isSelected()) {
						selected += weekdays[i].getName() + ",";
					}
				}
				if (selected.length() > 0) {
					selected = selected.substring(0, selected.length() - 1);
				}
				return selected;
			} 

			return null;
		}

		private void addWeekdays() {
			JPanel pane = new JPanel();
			weekdays[0] = new JCheckBox("S");
			weekdays[1] = new JCheckBox("M");
			weekdays[2] = new JCheckBox("T");
			weekdays[3] = new JCheckBox("W");
			weekdays[4] = new JCheckBox("R");
			weekdays[5] = new JCheckBox("F");
			weekdays[6] = new JCheckBox("U");

			weekdays[0].setName("SU");
			weekdays[1].setName("MO");
			weekdays[2].setName("TU");
			weekdays[3].setName("WE");
			weekdays[4].setName("TH");
			weekdays[5].setName("FR");
			weekdays[6].setName("SA");

			for (int i = 0; i < 7; i ++) {
				weekdays[i].setHorizontalTextPosition(SwingConstants.CENTER);
				weekdays[i].setVerticalTextPosition(SwingConstants.BOTTOM);			
				pane.add(weekdays[i]);

			}

			add(pane);
			add(Box.createHorizontalStrut(9));

		}

		/**
		 * Month box
		 */
		private void addMonths() {
			String[] months = {
					"Jan",
					"Feb",
					"Mar",
					"Apr",
					"May",
					"Jun",
					"Jul",
					"Aug",
					"Sep",
					"Oct",
					"Nov",
					"Dec"					
			};

			monthsList = new JComboBox(months);
			monthsList.setSelectedIndex(now.get(Calendar.MONTH));
			monthsList.setEditable(true);
			monthsList.addActionListener(this);

			ComboBoxEditor editor = monthsList.getEditor();
			timeMonth = (JTextField)editor.getEditorComponent();
			timeMonth.setColumns(3);

			c.gridx = 0;

			add(monthsList, c);	
		}


		/**
		 * Date Box
		 */
		private void addDays() {			
			String[] days = new String[31];
			for (int i = 0; i < 31; i ++) {
				if (i <= 8) {
					days[i] = "0" + (i + 1);
				} else {
					days[i] = "" + (i + 1);
				}			
			}

			daysList = new JComboBox(days);
			daysList.setSelectedIndex(now.get(Calendar.DAY_OF_MONTH) - 1);
			daysList.addActionListener(this);
			daysList.setEditable(true);

			ComboBoxEditor editor = daysList.getEditor();
			timeDay = (JTextField)editor.getEditorComponent();
			timeDay.setColumns(3);

			c.gridx = 1;

			add(daysList, c);			
		}

		/**
		 * Year Box
		 */
		private void addYears() {
			String[] years = new String[60];

			for (int i = 0; i < years.length; i ++) {
				years[i] = "" + (now.get(Calendar.YEAR) - 30 + i);				
			}

			yearsList = new JComboBox(years);
			yearsList.setSelectedIndex(years.length / 2);
			yearsList.setEditable(true);
			yearsList.addActionListener(this);

			ComboBoxEditor editor = yearsList.getEditor();
			timeYear = (JTextField)editor.getEditorComponent();
			timeYear.setColumns(3);

			c.gridx = 2;

			add(yearsList, c);

		}


		/**
		 * Hour Box 
		 */
		private void addHours() {
			String[] hours = new String[24];
			for (int i = 0; i < 24; i ++) {
				if (i <= 9) {
					hours[i] = "0" + i;
				} else {
					hours[i] = "" + i;
				}
			}

			hoursList = new JComboBox(hours);
			hoursList.setEditable(true);

			hoursList.setSelectedIndex(now.get(Calendar.HOUR_OF_DAY));
			hoursList.addActionListener(this);

			ComboBoxEditor editor = hoursList.getEditor();
			timeHour = (JTextField)editor.getEditorComponent();
			timeHour.setColumns(3);

			c.gridx = 4;

			add(hoursList, c);
		}


		/**
		 * Min Box
		 */
		private void addMinutes() {
			String[] minutes = new String[60];
			for (int i = 0; i < 60; i ++) {
				if (i <= 9) {
					minutes[i] = "0" + i;
				} else {
					minutes[i] = "" + i;
				}
			}

			minsList = new JComboBox(minutes);
			minsList.setEditable(true);
			if (now.get(Calendar.MINUTE) != 0) {
				minsList.setSelectedIndex(now.get(Calendar.MINUTE) - 1);
			} else {
				minsList.setSelectedIndex(now.get(Calendar.MINUTE));	
			}
			minsList.addActionListener(this);

			ComboBoxEditor editor = minsList.getEditor();
			timeMin = (JTextField)editor.getEditorComponent();
			timeMin.setColumns(3);


			c.gridx = 6;

			add(minsList, c);
		}

		/**
		 * Store the info from the date box to variables
		 * @param start true = startBox, false = endBox
		 */
		private void substitute(boolean start) {
			if (start) {
				startMonth = timeMonth;
				startDay = timeDay;
				startYear = timeYear;
				startHour = timeHour;
				startMin = timeMin;				
			} else {
				endMonth = timeMonth;
				endDay = timeDay;
				endYear = timeYear;
				endHour = timeHour;
				endMin = timeMin;	
			}

		}


		/**
		 * Check if the month is in correct format
		 * @param shortMonth
		 * @param longMonth
		 * @return
		 */
		private static boolean validMonth(String[] shortMonth, String[] longMonth) {
			if (Arrays.asList(shortMonth).contains(currentMonth) || Arrays.asList(longMonth).contains(currentMonth) || currentMonth.equals("Feb")) {
				return true;
			}
			if (Integer.parseInt(currentMonth) > 0 && Integer.parseInt(currentMonth) <= 12) {
				return true;
			}

			return false;
		}

		/**
		 * Check if date is in correct format
		 * @return
		 */
		private static boolean validDate() {
			if (Integer.parseInt(currentDate) > 0 && Integer.parseInt(currentDate) < daysList.getItemCount()) {
				return true;
			}
			return false;
		}


		/**
		 * Check if year is in correct format
		 * @return
		 */
		private static boolean validYear() {
			try {
				if (Integer.parseInt(currentYear) >= 0) {
					return true;
				}
			} catch (Exception e) {
				return false;
			}
			return false;

		}


		/**
		 * Check if hour and min are in correct format
		 * @return
		 */
		private static boolean validHrAndMin() {
			try {
				if (Integer.parseInt(currentHour) >= 0 && Integer.parseInt(currentHour) < 24 && Integer.parseInt(currentMin) >= 0 && Integer.parseInt(currentMin) < 60) {
					return true;
				} 				
			} catch (Exception e) {
				return false;
			}
			return false;
		}


		/**
		 * Check if the entire date is in correct format 
		 * @param shortMonth
		 * @param longMonth
		 * @return
		 */
		private static boolean validDateInput(String[] shortMonth, String[] longMonth) {
			try {
				if (validMonth(shortMonth, longMonth) &&  validDate() && validYear() && validHrAndMin()) {
					return true;
				}
			} catch (Exception e) {
				return false;
			}
			return false;		
		}

		/**
		 * Corresponding methods when the date box is changed 
		 */
		public void actionPerformed(ActionEvent e) {
			continueMessage();

			if (mode == 0) {
				currentMonth = timeMonth.getText();
				currentDate = timeDay.getText();
				currentYear = timeYear.getText();
				currentHour = timeHour.getText();
				currentMin = timeMin.getText();				

				//change the number of dates for each month
				if (Arrays.asList(longMonth).contains(currentMonth)) {
					while (daysList.getItemCount() != 31) {
						daysList.addItem("" + (daysList.getItemCount() + 1));						
					}				

					continueMessage();

				} else if (Arrays.asList(shortMonth).contains(currentMonth)) {
					if (daysList.getItemCount() == 31) {
						daysList.removeItemAt(30);
					}
					if (daysList.getItemCount() == 28) {
						daysList.addItem("29");					
					}
					if (daysList.getItemCount() == 29) {
						daysList.addItem("30");					
					}

					continueMessage();

				} else if (currentMonth.equals("Feb")) {
					while (daysList.getItemCount() != 28) {
						daysList.removeItemAt(daysList.getItemCount() - 1);
					}
					if (Integer.parseInt((String) yearsList.getSelectedItem()) % 4 == 0) {
						daysList.addItem("29");
					}					

					continueMessage();
				} 

				//check date format
				if (!validDateInput(shortMonth, longMonth)){
					message.setText("ERROR!!!! Please enter a valid date...");
					message.setForeground(Color.red);
					generate.setEnabled(false);			
				} else if (!checkTime(0)) {    //check if start time is earlier than end time
					message.setText("ERROR!!!! Start time is after end time...");
					message.setForeground(Color.red);	
					generate.setEnabled(false);		
				} else {
					continueMessage();
				}
			} else {
				currentHour = timeHour.getText();
				currentMin = timeMin.getText();
				if (!validHrAndMin()) {
					message.setText("ERROR!!!! Please enter a valid time...");
					message.setForeground(Color.red);
					generate.setEnabled(false);	
				} else if (!checkTime(1)) {
					message.setText("ERROR!!!! Start time is after end time...");
					message.setForeground(Color.red);	
					generate.setEnabled(false);		
				} else {
					continueMessage();
				}

			}

		}



	}


}